﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Web.Hosting;
using System.Xml.Linq;
using System.Xml.XPath;

namespace Skyhook.ProjectViewer.WebAspNet.Projects.AspGridView.Objects {
    public class AspGridView_Item {
        public AspGridView_Item() { }
        public AspGridView_Item(XElement xml) {
            Xml = xml;
            Map(xml);
        }
        public IEnumerable<AspGridView_Item> Map(XElement xml) {
            if (xml == null) return new List<AspGridView_Item>();
            var itemsXml = ((IEnumerable)xml.XPathEvaluate("./AspGridView_Item")).Cast<XElement>();

            return itemsXml.Select(itemXml => new AspGridView_Item {
                Id = Convert.ToInt32(itemXml.Attribute("Id").Value),
                CheckBoxColumn = Convert.ToBoolean(itemXml.Attribute("CheckBoxColumn").Value),
                Name = itemXml.Attribute("Name").Value,
                Description = itemXml.Attribute("Description").Value,
                ImageName = itemXml.Attribute("ImageName").Value
            });
        }

        private static string DataPath { get { return "~/Projects/AspGridView/Data"; } }

        public int Id { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public bool CheckBoxColumn { get; set; }
        public string ImageName { get; set; }

        public XElement Xml { get; set; }

        public static AspGridView_Item Get(int id) {
            return GetAll().Where(p => p.Id == id).Single();
        }
        public static List<AspGridView_Item> GetAll() {
            var templateFilesPaths = Directory.GetFiles(HostingEnvironment.MapPath(DataPath), "*.xml");
            return templateFilesPaths.Select(CreateProjectTemplate).ToList();
        }

        private static AspGridView_Item CreateProjectTemplate(string templateFilePath) {
            XElement xml;

            using (Stream fileStream = File.OpenRead(templateFilePath)) {
                xml = XDocument.Load(fileStream).Root;
            }

            var projectTemplate = new AspGridView_Item(xml);
            return projectTemplate;
        }

        public void Save() {
            var fileName = HostingEnvironment.MapPath(DataPath) + Id + ".xml";

            var xmlDocument = new XDocument(
                new XDeclaration("1.0", "utf-8", "yes"),
                new XElement("Root", new XAttribute("Type", "AspGridView_Items")));

            var rootNode = xmlDocument.Element("Root");

            var projectNode = new XElement("AspGridView_Item",
                new XAttribute("Id", Id.ToString()),
                new XAttribute("Name", Name),
                new XAttribute("Description", Description)
            );
            rootNode.Add(projectNode);

            if (!File.Exists(fileName)) {
                var xmlDocument_Create = new XDocument(
                new XDeclaration("1.0", "utf-8", "yes"),
                new XElement("Root", new XAttribute("Type", "AspGridView_Items")));

                xmlDocument_Create.Save(fileName);
            }

            using (Stream fileStream = File.Create(fileName)) {
                xmlDocument.Save(fileStream);
            }

            Xml = xmlDocument.Root;
        }

        public void Delete() {
            var fileName = HostingEnvironment.MapPath(DataPath) + Id + ".xml";
            var fileInfo = new FileInfo(fileName);
            fileInfo.Delete();
            fileInfo.Refresh();
        }
    }
}
